package br.facet.dao;

import org.hibernate.Session;
import org.hibernate.Transaction;

import br.facet.pojo.Endereco;
import br.facet.utils.HibernateUtils;

public class EnderecoDAO {
	
	
	 public void salvar(Endereco endereco) throws Exception {
	    	Session sessao = null;
			Transaction tx = null;
	    	
			try {
				sessao = HibernateUtils.getSession();
				tx = sessao.beginTransaction();
				sessao.save(endereco);
				tx.commit();
	            
	        } catch (Exception e) {
	        	tx.rollback();
	        	e.printStackTrace();
	        } finally {
	        	if(sessao != null){
	        		try {
	        			sessao.close();					
					} catch (Exception e2) {
						e2.printStackTrace();
					}
	        	}
	        }

	    }
	 
	 public Endereco load(Integer id) throws Exception {
			Endereco result = null;
			Session session = HibernateUtils.getSessionFactory().openSession();
			try {
				result = (Endereco) session.load(Endereco.class, id);
			} 
			catch (Exception e) {
				session.getTransaction().rollback();
				throw new Exception("Erro ao carregar o Produto. Tente novamente.");
			} finally {
				session.close();
			}
			return result;
		}

}
